<?php

namespace app\aiv110\controller;

use app\aiv110\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\aiv110\model\Facecg;
use app\util\Baidufacecg;
use app\util\File;

class FacecgController extends CommonController {
    /*
     * code列表
     */

    public function code() {
        /*
         * 0  成功
         * 1  参数错误
         * 2  验证失败
         * 120000  
         * 120001  
         * 120002  
         * 120003
         * 120004
         */
    }
    
    /*
     * 人脸融合模板列表
     * author xfwed 
     * date 2021.01.23
     * 
     * @param int perPage 每页数量 非必须，默认20
     * @param int page 当前页数 非必须，默认0
     */

    public function facecgList() {
        $rule = [
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:0',
        ];
        $msg = [
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '页数为数字',
            'page.egt' => '页数最小为0',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }

        $offset = ($page - 1) * $perPage;
        
        $where['status'] = 2;
        $list = Facecg::where($where)
                ->order('use_num desc,id desc')
                ->limit($offset, $perPage)
                ->select();
        return Utils::ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 人脸融合模板内容条
     * author xfwed 
     * date 2021.01.23
     */

    public function facecgInfo() {
        $ctm_id = input('param.ctm_id');
        $arr=[
            '0'=>[//默认的
                'img'=>'http://47.104.83.107/images/kkmimg/kkmai/default/facecginfo/default.png',
                ],
            '20210211'=>[
                'img'=>'http://47.104.83.107/images/kkmimg/kkmai/default/facecginfo/20210211.png',
                ],
            '20210212'=>[
                'img'=>'http://47.104.83.107/images/kkmimg/kkmai/default/facecginfo/20210212.png',
                ],
            '20210213'=>[
                'img'=>'http://47.104.83.107/images/kkmimg/kkmai/default/facecginfo/20210213.png',
                ],
            '20210214'=>[
                'img'=>'http://47.104.83.107/images/kkmimg/kkmai/default/facecginfo/20210214.png',
                ],
            '20210215'=>[
                'img'=>'http://47.104.83.107/images/kkmimg/kkmai/default/facecginfo/20210215.png',
                ],
            '20210216'=>[
                'img'=>'http://47.104.83.107/images/kkmimg/kkmai/default/facecginfo/20210216.png',
                ],
            '20210217'=>[
                'img'=>'http://47.104.83.107/images/kkmimg/kkmai/default/facecginfo/20210217.png',
                ],
            '20210218'=>[
                'img'=>'http://47.104.83.107/images/kkmimg/kkmai/default/facecginfo/20210218.png',
                ],
        ];
        $a = date('Y').date('m').date('d');
        if(empty($arr[$a])){
            $data = $arr[0];
        }  else {
            $data = $arr[$a];
        }
        if($ctm_id){
            $yuyueRow = Db::name('yuyue')
                    ->where('ctm_id',$ctm_id)
                    ->where('status','<>',6)
                    ->find();
            if($yuyueRow){
                $is_new = 2;//1是2否为新人
            } else {
                $is_new = 1;//1是2否为新人
            }
        } else {
            $is_new = 1;//1是2否为新人
        }
        $result['img'] = $data;
        $result['is_new'] = $is_new;
        return Utils::ajaxReturn(0, '查询成功', $result);
    }
    
    
    /*
     * 人脸融合
     * author xfwed 
     * date 2021.01.23
     * 
     * @param int perPage 每页数量 非必须，默认20
     * @param int page 当前页数 非必须，默认0
     */

    public function facecg() {
        $rule = [
            'srca' => 'require',
            'srcb' => 'require',
        ];
        $msg = [
            'srca.require' => '模板图必须',
            'srcb.require' => '用户照片必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }
        
        $srca = $all['srca'];
        $srcb = $all['srcb'];
        
        //将图片转为base64编码
        $imageInfo = getimagesize($srca);
        $base64 = "" . chunk_split(base64_encode(file_get_contents($srca)));
        //$base64_srca = 'data:' . $imageInfo['mime'] . ';base64,' . chunk_split(base64_encode(file_get_contents($srca)));
        $base64_srca = chunk_split(base64_encode(file_get_contents($srca)));
        $imgwidth = $imageInfo[0];
        $imgheight = $imageInfo[1];
        if($imgwidth >= $imgheight){
            if($imgwidth > 1920){
                return Utils::ajaxReturn(1, '模板图片尺寸太大', null);
            }
        }  else {
            if($imgheight > 1920){
                return Utils::ajaxReturn(1, '图片尺寸太大', null);
            }
        }
        
        $imagebInfo = getimagesize($srcb);
        $base64 = "" . chunk_split(base64_encode(file_get_contents($srcb)));
        $base64_srcb = chunk_split(base64_encode(file_get_contents($srcb)));
        $imgbwidth = $imagebInfo[0];
        $imgbheight = $imagebInfo[1];
        if($imgbwidth >= $imgbheight){
            if($imgbwidth > 1920){
                return Utils::ajaxReturn(1, '图片尺寸太大', null);
            }
        }  else {
            if($imgbheight > 1920){
                return Utils::ajaxReturn(1, '图片尺寸太大', null);
            }
        }
        $srca = str_replace("https://www.kkmimg.com","http://47.104.83.107",$srca);
        Facecg::where('img',$srca)->setInc('use_num',1);
        
        $facecg = new Baidufacecg();
        $data = $facecg->facecg($base64_srca,$base64_srcb);
        return json_encode($data);
    }
    
}
